using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Win32;

namespace sforceExplorer
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class frmMain : System.Windows.Forms.Form
	{
		#region Local Variable declarations
		//private LogTraceListener Listener;
		private SOQLPlus sPlus = new SOQLPlus();
		private string sPlusCommand = "";
		private System.Text.StringBuilder sPlusBuffer = new System.Text.StringBuilder();
		public delegate void AddToTreeViewDelegate(System.Windows.Forms.TreeNode node);
		public delegate void AddSubNodeToTreeViewDelegate(System.Windows.Forms.TreeNode parent, System.Windows.Forms.TreeNode child);
		public delegate void AddToErrorLogDelegate(String errorMessage);
		private System.Threading.Thread dbRepThread;

		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox txtSOQL;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Button btnRunQuery;
		private System.Windows.Forms.TextBox txtResults;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.Panel panel3;
		private System.Windows.Forms.Splitter splitter1;
		private System.Windows.Forms.Panel panel4;
		private System.Windows.Forms.Splitter splitter2;
		private System.Windows.Forms.Panel panel5;
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.Windows.Forms.ProgressBar progressBar1;
		private System.Windows.Forms.Label lblSummary;
		private System.Windows.Forms.Button btnMore;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItem2;
        private System.Windows.Forms.TabControl tabControl1;
        private System.Windows.Forms.MenuItem menuItem3;
        private System.Windows.Forms.MenuItem menuItem5;
		private System.Windows.Forms.Button btnRefresh;
		private System.Windows.Forms.Button btnLogin;
		private System.Windows.Forms.MenuItem mnuSamples;
		private System.Windows.Forms.MenuItem mnuSamplesAddAccount;
		private System.Windows.Forms.MenuItem mnuSamplesResetPassword;
		private System.Windows.Forms.MenuItem mnuSamplesSetNewPassword;
		private System.Windows.Forms.MenuItem mnuSamplesGetTimeStamp;
		private System.Windows.Forms.MenuItem mnuSamplesShowUserInfo;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.ProgressBar progressBar2;
		private System.Windows.Forms.ComboBox cboSearchGroup;
		private System.Windows.Forms.ImageList imageList1;
		private System.Windows.Forms.TextBox txtSearchQuery;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Panel panel6;
		private System.Windows.Forms.Splitter splitter3;
		private System.Windows.Forms.Panel panel7;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Panel panel8;
		private System.Windows.Forms.Panel panel9;
		private System.Windows.Forms.Panel panel10;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.TreeView tvRes;
		private System.Windows.Forms.Splitter splitter4;
		private System.Windows.Forms.Panel panel11;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.TextBox txtFinalSOSL;
		private System.Windows.Forms.Splitter splitter5;
		private System.Windows.Forms.Panel panel12;
		private System.Windows.Forms.Label label10;
		private System.Windows.Forms.TextBox txtReturnFields;
		private System.Windows.Forms.Label label11;
		private System.Windows.Forms.Button btnSend;
        private System.Windows.Forms.TabPage tabSOQL;
		private System.Windows.Forms.TabPage tabSOSL;
		private System.Windows.Forms.Button btnRefreshSOSL;
		private System.Windows.Forms.TabPage tabEditor;
		private ApiBrowserControl apiBrowserControl1;
		private ApiBrowserControl apiBrowserControl2;
		private frmEditList frmEditList1;
		private System.Windows.Forms.StatusBar statusBar1;
		private System.Windows.Forms.StatusBarPanel sbpLoggedIn;
        private System.Windows.Forms.StatusBarPanel sbpURL;
		private System.Windows.Forms.TabPage tabDocuments;
        private DocumentControl.DocumentControl documentControl1;
		private System.Windows.Forms.Label lblElapsedTime;
        protected internal System.Windows.Forms.ToolTip toolTip1;
		private System.Windows.Forms.ContextMenu contextMenu1;
		private System.Windows.Forms.MenuItem menuSControlEdit;
		private System.Windows.Forms.MenuItem menuSControlDelete;
		private System.Windows.Forms.MenuItem menuSControlPaste;
		private sforce.QueryResult lastQuery;
		#endregion
		
		public frmMain()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			GlobalProperties.CheckEncryption();
			//initially hide all tabs
			this.tabControl1.Controls.Remove(this.tabSOQL);
			this.tabControl1.Controls.Remove(this.tabSOSL);
			this.tabControl1.Controls.Remove(this.tabEditor);
			this.tabControl1.Controls.Remove(this.tabDocuments);
			GlobalProperties.GetRegistrySettings();

			//This sets the default endpoint to the one on the generated proxy.
			GlobalProperties.endPoint = Utility.binding.Url;

			//add handlers for the browser controls
			this.apiBrowserControl1.SelectListChanged +=new SelectListChangedEventHandler(apiBrowserControl1_SelectListChanged);
			this.apiBrowserControl2.SelectListChanged +=new SelectListChangedEventHandler(apiBrowserControl2_SelectListChanged);
			this.apiBrowserControl1.MultiObjectSelect = false;

			//restore user settings from registry values
			this.menuItem3.Checked = GlobalProperties.useCompression;
			this.menuItem5.Checked = GlobalProperties.useProxy;


			//add other event handlers on the form
			this.Closed +=new EventHandler(frmMain_Closed);
			this.Activated -= new EventHandler(frmMain_Activated);
			this.Activated +=new EventHandler(frmMain_Activated);
			this.tabControl1.TabIndexChanged +=new EventHandler(tabControl1_TabIndexChanged);
			this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);

			//Check to see if we are using a session id instead of username password
			this.btnLogin.Enabled = !GlobalProperties.useSessionId;
			this.cboSearchGroup.SelectedIndex = 0;

			//add document control and query timing handlers
			this.documentControl1.DocumentSelected +=new DocumentControl.DocumentControl.DocumentSelectedEventHandler(documentControl1_DocumentSelected);
			Utility.binding.OnQueryComplete+=new QueryTiming(binding_OnQueryComplete);
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmMain));
            this.label3 = new System.Windows.Forms.Label();
            this.txtSOQL = new System.Windows.Forms.TextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.btnRunQuery = new System.Windows.Forms.Button();
            this.txtResults = new System.Windows.Forms.TextBox();
            this.panel1 = new System.Windows.Forms.Panel();
            this.panel3 = new System.Windows.Forms.Panel();
            this.apiBrowserControl1 = new sforceExplorer.ApiBrowserControl();
            this.btnRefresh = new System.Windows.Forms.Button();
            this.splitter1 = new System.Windows.Forms.Splitter();
            this.panel2 = new System.Windows.Forms.Panel();
            this.panel5 = new System.Windows.Forms.Panel();
            this.btnMore = new System.Windows.Forms.Button();
            this.lblSummary = new System.Windows.Forms.Label();
            this.progressBar1 = new System.Windows.Forms.ProgressBar();
            this.dataGrid1 = new System.Windows.Forms.DataGrid();
            this.splitter2 = new System.Windows.Forms.Splitter();
            this.panel4 = new System.Windows.Forms.Panel();
            this.lblElapsedTime = new System.Windows.Forms.Label();
            this.mainMenu1 = new System.Windows.Forms.MainMenu(this.components);
            this.menuItem1 = new System.Windows.Forms.MenuItem();
            this.menuItem2 = new System.Windows.Forms.MenuItem();
            this.menuItem3 = new System.Windows.Forms.MenuItem();
            this.menuItem5 = new System.Windows.Forms.MenuItem();
            this.mnuSamples = new System.Windows.Forms.MenuItem();
            this.mnuSamplesAddAccount = new System.Windows.Forms.MenuItem();
            this.mnuSamplesResetPassword = new System.Windows.Forms.MenuItem();
            this.mnuSamplesSetNewPassword = new System.Windows.Forms.MenuItem();
            this.mnuSamplesGetTimeStamp = new System.Windows.Forms.MenuItem();
            this.mnuSamplesShowUserInfo = new System.Windows.Forms.MenuItem();
            this.tabControl1 = new System.Windows.Forms.TabControl();
            this.tabSOQL = new System.Windows.Forms.TabPage();
            this.tabSOSL = new System.Windows.Forms.TabPage();
            this.progressBar2 = new System.Windows.Forms.ProgressBar();
            this.cboSearchGroup = new System.Windows.Forms.ComboBox();
            this.txtSearchQuery = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.panel6 = new System.Windows.Forms.Panel();
            this.panel7 = new System.Windows.Forms.Panel();
            this.apiBrowserControl2 = new sforceExplorer.ApiBrowserControl();
            this.btnRefreshSOSL = new System.Windows.Forms.Button();
            this.label5 = new System.Windows.Forms.Label();
            this.splitter3 = new System.Windows.Forms.Splitter();
            this.panel8 = new System.Windows.Forms.Panel();
            this.panel9 = new System.Windows.Forms.Panel();
            this.panel10 = new System.Windows.Forms.Panel();
            this.label6 = new System.Windows.Forms.Label();
            this.tvRes = new System.Windows.Forms.TreeView();
            this.imageList1 = new System.Windows.Forms.ImageList(this.components);
            this.splitter4 = new System.Windows.Forms.Splitter();
            this.panel11 = new System.Windows.Forms.Panel();
            this.label7 = new System.Windows.Forms.Label();
            this.txtFinalSOSL = new System.Windows.Forms.TextBox();
            this.splitter5 = new System.Windows.Forms.Splitter();
            this.panel12 = new System.Windows.Forms.Panel();
            this.label10 = new System.Windows.Forms.Label();
            this.txtReturnFields = new System.Windows.Forms.TextBox();
            this.label11 = new System.Windows.Forms.Label();
            this.btnSend = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.tabEditor = new System.Windows.Forms.TabPage();
            this.frmEditList1 = new sforceExplorer.frmEditList();
            this.tabDocuments = new System.Windows.Forms.TabPage();
            this.documentControl1 = new DocumentControl.DocumentControl();
            this.contextMenu1 = new System.Windows.Forms.ContextMenu();
            this.menuSControlEdit = new System.Windows.Forms.MenuItem();
            this.menuSControlDelete = new System.Windows.Forms.MenuItem();
            this.menuSControlPaste = new System.Windows.Forms.MenuItem();
            this.btnLogin = new System.Windows.Forms.Button();
            this.statusBar1 = new System.Windows.Forms.StatusBar();
            this.sbpLoggedIn = new System.Windows.Forms.StatusBarPanel();
            this.sbpURL = new System.Windows.Forms.StatusBarPanel();
            this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
            this.panel1.SuspendLayout();
            this.panel3.SuspendLayout();
            this.panel2.SuspendLayout();
            this.panel5.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
            this.panel4.SuspendLayout();
            this.tabControl1.SuspendLayout();
            this.tabSOQL.SuspendLayout();
            this.tabSOSL.SuspendLayout();
            this.panel6.SuspendLayout();
            this.panel7.SuspendLayout();
            this.panel8.SuspendLayout();
            this.panel9.SuspendLayout();
            this.panel10.SuspendLayout();
            this.panel11.SuspendLayout();
            this.panel12.SuspendLayout();
            this.tabEditor.SuspendLayout();
            this.tabDocuments.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.sbpLoggedIn)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.sbpURL)).BeginInit();
            this.SuspendLayout();
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(0, 16);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(104, 16);
            this.label3.TabIndex = 2;
            this.label3.Text = "Describe Results";
            // 
            // txtSOQL
            // 
            this.txtSOQL.Dock = System.Windows.Forms.DockStyle.Fill;
            this.txtSOQL.Enabled = false;
            this.txtSOQL.Location = new System.Drawing.Point(0, 40);
            this.txtSOQL.Multiline = true;
            this.txtSOQL.Name = "txtSOQL";
            this.txtSOQL.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.txtSOQL.Size = new System.Drawing.Size(432, 80);
            this.txtSOQL.TabIndex = 7;
            this.txtSOQL.Text = "Select Id, LastName from Contact";
            // 
            // label4
            // 
            this.label4.Location = new System.Drawing.Point(0, 16);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(72, 16);
            this.label4.TabIndex = 2;
            this.label4.Text = "Enter SOQL";
            // 
            // btnRunQuery
            // 
            this.btnRunQuery.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btnRunQuery.Enabled = false;
            this.btnRunQuery.Location = new System.Drawing.Point(344, 8);
            this.btnRunQuery.Name = "btnRunQuery";
            this.btnRunQuery.Size = new System.Drawing.Size(80, 24);
            this.btnRunQuery.TabIndex = 8;
            this.btnRunQuery.Text = "Run Query";
            this.btnRunQuery.Click += new System.EventHandler(this.btnRunQuery_Click);
            // 
            // txtResults
            // 
            this.txtResults.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.txtResults.BackColor = System.Drawing.Color.Black;
            this.txtResults.ForeColor = System.Drawing.Color.White;
            this.txtResults.Location = new System.Drawing.Point(8, 8);
            this.txtResults.Multiline = true;
            this.txtResults.Name = "txtResults";
            this.txtResults.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.txtResults.Size = new System.Drawing.Size(416, 56);
            this.txtResults.TabIndex = 9;
            this.txtResults.Visible = false;
            // 
            // panel1
            // 
            this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.panel1.Controls.Add(this.panel3);
            this.panel1.Controls.Add(this.splitter1);
            this.panel1.Controls.Add(this.panel2);
            this.panel1.Location = new System.Drawing.Point(8, 8);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(664, 472);
            this.panel1.TabIndex = 11;
            // 
            // panel3
            // 
            this.panel3.Controls.Add(this.apiBrowserControl1);
            this.panel3.Controls.Add(this.btnRefresh);
            this.panel3.Controls.Add(this.label3);
            this.panel3.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel3.Location = new System.Drawing.Point(436, 0);
            this.panel3.Name = "panel3";
            this.panel3.Padding = new System.Windows.Forms.Padding(0, 40, 0, 0);
            this.panel3.Size = new System.Drawing.Size(228, 472);
            this.panel3.TabIndex = 1;
            // 
            // apiBrowserControl1
            // 
            this.apiBrowserControl1.CurrentObject = null;
            this.apiBrowserControl1.CurrentRootNode = null;
            this.apiBrowserControl1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.apiBrowserControl1.Location = new System.Drawing.Point(0, 40);
            this.apiBrowserControl1.MultiObjectSelect = false;
            this.apiBrowserControl1.Name = "apiBrowserControl1";
            this.apiBrowserControl1.Size = new System.Drawing.Size(228, 432);
            this.apiBrowserControl1.TabIndex = 8;
            // 
            // btnRefresh
            // 
            this.btnRefresh.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btnRefresh.Location = new System.Drawing.Point(136, 8);
            this.btnRefresh.Name = "btnRefresh";
            this.btnRefresh.Size = new System.Drawing.Size(80, 24);
            this.btnRefresh.TabIndex = 7;
            this.btnRefresh.Text = "Refresh";
            this.btnRefresh.Click += new System.EventHandler(this.btnRefresh_Click);
            // 
            // splitter1
            // 
            this.splitter1.BackColor = System.Drawing.SystemColors.Control;
            this.splitter1.Location = new System.Drawing.Point(432, 0);
            this.splitter1.Name = "splitter1";
            this.splitter1.Size = new System.Drawing.Size(4, 472);
            this.splitter1.TabIndex = 2;
            this.splitter1.TabStop = false;
            // 
            // panel2
            // 
            this.panel2.Controls.Add(this.panel5);
            this.panel2.Controls.Add(this.splitter2);
            this.panel2.Controls.Add(this.panel4);
            this.panel2.Dock = System.Windows.Forms.DockStyle.Left;
            this.panel2.Location = new System.Drawing.Point(0, 0);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(432, 472);
            this.panel2.TabIndex = 0;
            // 
            // panel5
            // 
            this.panel5.Controls.Add(this.btnMore);
            this.panel5.Controls.Add(this.lblSummary);
            this.panel5.Controls.Add(this.progressBar1);
            this.panel5.Controls.Add(this.dataGrid1);
            this.panel5.Controls.Add(this.txtResults);
            this.panel5.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel5.Location = new System.Drawing.Point(0, 124);
            this.panel5.Name = "panel5";
            this.panel5.Padding = new System.Windows.Forms.Padding(0, 0, 0, 30);
            this.panel5.Size = new System.Drawing.Size(432, 348);
            this.panel5.TabIndex = 2;
            // 
            // btnMore
            // 
            this.btnMore.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.btnMore.Location = new System.Drawing.Point(112, 328);
            this.btnMore.Name = "btnMore";
            this.btnMore.Size = new System.Drawing.Size(48, 16);
            this.btnMore.TabIndex = 13;
            this.btnMore.Text = "more..";
            this.btnMore.Visible = false;
            this.btnMore.Click += new System.EventHandler(this.btnMore_Click);
            // 
            // lblSummary
            // 
            this.lblSummary.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.lblSummary.Location = new System.Drawing.Point(0, 328);
            this.lblSummary.Name = "lblSummary";
            this.lblSummary.Size = new System.Drawing.Size(104, 16);
            this.lblSummary.TabIndex = 12;
            // 
            // progressBar1
            // 
            this.progressBar1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.progressBar1.Location = new System.Drawing.Point(168, 328);
            this.progressBar1.Name = "progressBar1";
            this.progressBar1.Size = new System.Drawing.Size(263, 16);
            this.progressBar1.TabIndex = 11;
            // 
            // dataGrid1
            // 
            this.dataGrid1.DataMember = "";
            this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.dataGrid1.Location = new System.Drawing.Point(0, 0);
            this.dataGrid1.Name = "dataGrid1";
            this.dataGrid1.Size = new System.Drawing.Size(432, 318);
            this.dataGrid1.TabIndex = 10;
            // 
            // splitter2
            // 
            this.splitter2.BackColor = System.Drawing.SystemColors.Control;
            this.splitter2.Cursor = System.Windows.Forms.Cursors.HSplit;
            this.splitter2.Dock = System.Windows.Forms.DockStyle.Top;
            this.splitter2.Location = new System.Drawing.Point(0, 120);
            this.splitter2.Name = "splitter2";
            this.splitter2.Size = new System.Drawing.Size(432, 4);
            this.splitter2.TabIndex = 1;
            this.splitter2.TabStop = false;
            // 
            // panel4
            // 
            this.panel4.Controls.Add(this.lblElapsedTime);
            this.panel4.Controls.Add(this.label4);
            this.panel4.Controls.Add(this.btnRunQuery);
            this.panel4.Controls.Add(this.txtSOQL);
            this.panel4.Dock = System.Windows.Forms.DockStyle.Top;
            this.panel4.Location = new System.Drawing.Point(0, 0);
            this.panel4.Name = "panel4";
            this.panel4.Padding = new System.Windows.Forms.Padding(0, 40, 0, 0);
            this.panel4.Size = new System.Drawing.Size(432, 120);
            this.panel4.TabIndex = 0;
            // 
            // lblElapsedTime
            // 
            this.lblElapsedTime.Location = new System.Drawing.Point(88, 16);
            this.lblElapsedTime.Name = "lblElapsedTime";
            this.lblElapsedTime.Size = new System.Drawing.Size(216, 16);
            this.lblElapsedTime.TabIndex = 9;
            this.lblElapsedTime.Text = "Elapsed Time:";
            // 
            // mainMenu1
            // 
            this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem1,
            this.mnuSamples});
            // 
            // menuItem1
            // 
            this.menuItem1.Index = 0;
            this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem2,
            this.menuItem3,
            this.menuItem5});
            this.menuItem1.Text = "Tools";
            // 
            // menuItem2
            // 
            this.menuItem2.Index = 0;
            this.menuItem2.Text = "Options";
            this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
            // 
            // menuItem3
            // 
            this.menuItem3.Index = 1;
            this.menuItem3.RadioCheck = true;
            this.menuItem3.Text = "Enable Compression";
            this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
            // 
            // menuItem5
            // 
            this.menuItem5.Index = 2;
            this.menuItem5.RadioCheck = true;
            this.menuItem5.Text = "Use Proxy";
            // 
            // mnuSamples
            // 
            this.mnuSamples.Index = 1;
            this.mnuSamples.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.mnuSamplesAddAccount,
            this.mnuSamplesResetPassword,
            this.mnuSamplesSetNewPassword,
            this.mnuSamplesGetTimeStamp,
            this.mnuSamplesShowUserInfo});
            this.mnuSamples.Text = "Sample Calls";
            // 
            // mnuSamplesAddAccount
            // 
            this.mnuSamplesAddAccount.Enabled = false;
            this.mnuSamplesAddAccount.Index = 0;
            this.mnuSamplesAddAccount.Text = "&Add Account";
            this.mnuSamplesAddAccount.Click += new System.EventHandler(this.mnuSamplesAddAccount_Click);
            // 
            // mnuSamplesResetPassword
            // 
            this.mnuSamplesResetPassword.Enabled = false;
            this.mnuSamplesResetPassword.Index = 1;
            this.mnuSamplesResetPassword.Text = "&Reset Password";
            this.mnuSamplesResetPassword.Click += new System.EventHandler(this.mnuSamplesResetPassword_Click);
            // 
            // mnuSamplesSetNewPassword
            // 
            this.mnuSamplesSetNewPassword.Enabled = false;
            this.mnuSamplesSetNewPassword.Index = 2;
            this.mnuSamplesSetNewPassword.Text = "Set &New Password";
            this.mnuSamplesSetNewPassword.Click += new System.EventHandler(this.mnuSamplesSetNewPassword_Click);
            // 
            // mnuSamplesGetTimeStamp
            // 
            this.mnuSamplesGetTimeStamp.Enabled = false;
            this.mnuSamplesGetTimeStamp.Index = 3;
            this.mnuSamplesGetTimeStamp.Text = "&Get Timestamp";
            this.mnuSamplesGetTimeStamp.Click += new System.EventHandler(this.mnuSamplesGetTimeStamp_Click);
            // 
            // mnuSamplesShowUserInfo
            // 
            this.mnuSamplesShowUserInfo.Enabled = false;
            this.mnuSamplesShowUserInfo.Index = 4;
            this.mnuSamplesShowUserInfo.Text = "Show &User Info";
            this.mnuSamplesShowUserInfo.Click += new System.EventHandler(this.mnuSamplesShowUserInfo_Click);
            // 
            // tabControl1
            // 
            this.tabControl1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.tabControl1.Controls.Add(this.tabSOQL);
            this.tabControl1.Controls.Add(this.tabSOSL);
            this.tabControl1.Controls.Add(this.tabEditor);
            this.tabControl1.Controls.Add(this.tabDocuments);
            this.tabControl1.Location = new System.Drawing.Point(0, 37);
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new System.Drawing.Size(688, 513);
            this.tabControl1.TabIndex = 12;
            // 
            // tabSOQL
            // 
            this.tabSOQL.Controls.Add(this.panel1);
            this.tabSOQL.Location = new System.Drawing.Point(4, 22);
            this.tabSOQL.Name = "tabSOQL";
            this.tabSOQL.Size = new System.Drawing.Size(680, 487);
            this.tabSOQL.TabIndex = 0;
            this.tabSOQL.Text = "SOQL Tester";
            // 
            // tabSOSL
            // 
            this.tabSOSL.Controls.Add(this.progressBar2);
            this.tabSOSL.Controls.Add(this.cboSearchGroup);
            this.tabSOSL.Controls.Add(this.txtSearchQuery);
            this.tabSOSL.Controls.Add(this.label2);
            this.tabSOSL.Controls.Add(this.panel6);
            this.tabSOSL.Controls.Add(this.label11);
            this.tabSOSL.Controls.Add(this.btnSend);
            this.tabSOSL.Controls.Add(this.label1);
            this.tabSOSL.Location = new System.Drawing.Point(4, 22);
            this.tabSOSL.Name = "tabSOSL";
            this.tabSOSL.Size = new System.Drawing.Size(680, 487);
            this.tabSOSL.TabIndex = 2;
            this.tabSOSL.Text = "SOSL Tester";
            // 
            // progressBar2
            // 
            this.progressBar2.Location = new System.Drawing.Point(376, 48);
            this.progressBar2.Name = "progressBar2";
            this.progressBar2.Size = new System.Drawing.Size(288, 16);
            this.progressBar2.TabIndex = 25;
            // 
            // cboSearchGroup
            // 
            this.cboSearchGroup.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cboSearchGroup.Items.AddRange(new object[] {
            "Global",
            "All Fields",
            "Email Fields",
            "Name Fields",
            "Phone Fields"});
            this.cboSearchGroup.Location = new System.Drawing.Point(120, 48);
            this.cboSearchGroup.Name = "cboSearchGroup";
            this.cboSearchGroup.Size = new System.Drawing.Size(240, 21);
            this.cboSearchGroup.TabIndex = 22;
            this.cboSearchGroup.SelectedIndexChanged += new System.EventHandler(this.cboSearchGroup_SelectedIndexChanged);
            // 
            // txtSearchQuery
            // 
            this.txtSearchQuery.Location = new System.Drawing.Point(120, 16);
            this.txtSearchQuery.Name = "txtSearchQuery";
            this.txtSearchQuery.Size = new System.Drawing.Size(240, 20);
            this.txtSearchQuery.TabIndex = 19;
            this.txtSearchQuery.TextChanged += new System.EventHandler(this.txtSearchQuery_TextChanged);
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(8, 53);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(112, 16);
            this.label2.TabIndex = 20;
            this.label2.Text = "Select Search Group";
            // 
            // panel6
            // 
            this.panel6.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.panel6.Controls.Add(this.panel7);
            this.panel6.Controls.Add(this.splitter3);
            this.panel6.Controls.Add(this.panel8);
            this.panel6.Location = new System.Drawing.Point(8, 80);
            this.panel6.Name = "panel6";
            this.panel6.Size = new System.Drawing.Size(664, 397);
            this.panel6.TabIndex = 24;
            // 
            // panel7
            // 
            this.panel7.Controls.Add(this.apiBrowserControl2);
            this.panel7.Controls.Add(this.btnRefreshSOSL);
            this.panel7.Controls.Add(this.label5);
            this.panel7.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel7.Location = new System.Drawing.Point(412, 0);
            this.panel7.Name = "panel7";
            this.panel7.Padding = new System.Windows.Forms.Padding(0, 30, 0, 0);
            this.panel7.Size = new System.Drawing.Size(252, 397);
            this.panel7.TabIndex = 1;
            // 
            // apiBrowserControl2
            // 
            this.apiBrowserControl2.CurrentObject = null;
            this.apiBrowserControl2.CurrentRootNode = null;
            this.apiBrowserControl2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.apiBrowserControl2.Location = new System.Drawing.Point(0, 30);
            this.apiBrowserControl2.MultiObjectSelect = false;
            this.apiBrowserControl2.Name = "apiBrowserControl2";
            this.apiBrowserControl2.Size = new System.Drawing.Size(252, 367);
            this.apiBrowserControl2.TabIndex = 10;
            // 
            // btnRefreshSOSL
            // 
            this.btnRefreshSOSL.Location = new System.Drawing.Point(152, 0);
            this.btnRefreshSOSL.Name = "btnRefreshSOSL";
            this.btnRefreshSOSL.Size = new System.Drawing.Size(88, 24);
            this.btnRefreshSOSL.TabIndex = 9;
            this.btnRefreshSOSL.Text = "Refresh";
            this.btnRefreshSOSL.Click += new System.EventHandler(this.btnRefreshSOSL_Click);
            // 
            // label5
            // 
            this.label5.Location = new System.Drawing.Point(0, 8);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(96, 16);
            this.label5.TabIndex = 7;
            this.label5.Text = "Describe Results";
            // 
            // splitter3
            // 
            this.splitter3.Location = new System.Drawing.Point(408, 0);
            this.splitter3.Name = "splitter3";
            this.splitter3.Size = new System.Drawing.Size(4, 397);
            this.splitter3.TabIndex = 2;
            this.splitter3.TabStop = false;
            // 
            // panel8
            // 
            this.panel8.Controls.Add(this.panel9);
            this.panel8.Controls.Add(this.splitter5);
            this.panel8.Controls.Add(this.panel12);
            this.panel8.Dock = System.Windows.Forms.DockStyle.Left;
            this.panel8.Location = new System.Drawing.Point(0, 0);
            this.panel8.Name = "panel8";
            this.panel8.Size = new System.Drawing.Size(408, 397);
            this.panel8.TabIndex = 0;
            // 
            // panel9
            // 
            this.panel9.Controls.Add(this.panel10);
            this.panel9.Controls.Add(this.splitter4);
            this.panel9.Controls.Add(this.panel11);
            this.panel9.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel9.Location = new System.Drawing.Point(0, 108);
            this.panel9.Name = "panel9";
            this.panel9.Size = new System.Drawing.Size(408, 289);
            this.panel9.TabIndex = 2;
            // 
            // panel10
            // 
            this.panel10.Controls.Add(this.label6);
            this.panel10.Controls.Add(this.tvRes);
            this.panel10.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel10.Location = new System.Drawing.Point(0, 108);
            this.panel10.Name = "panel10";
            this.panel10.Padding = new System.Windows.Forms.Padding(0, 25, 0, 0);
            this.panel10.Size = new System.Drawing.Size(408, 181);
            this.panel10.TabIndex = 2;
            // 
            // label6
            // 
            this.label6.Location = new System.Drawing.Point(0, 8);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(112, 16);
            this.label6.TabIndex = 10;
            this.label6.Text = "Results";
            // 
            // tvRes
            // 
            this.tvRes.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tvRes.ImageIndex = 0;
            this.tvRes.ImageList = this.imageList1;
            this.tvRes.Location = new System.Drawing.Point(0, 25);
            this.tvRes.Name = "tvRes";
            this.tvRes.SelectedImageIndex = 0;
            this.tvRes.Size = new System.Drawing.Size(408, 156);
            this.tvRes.TabIndex = 15;
            // 
            // imageList1
            // 
            this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
            this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
            this.imageList1.Images.SetKeyName(0, "");
            this.imageList1.Images.SetKeyName(1, "");
            this.imageList1.Images.SetKeyName(2, "");
            this.imageList1.Images.SetKeyName(3, "");
            this.imageList1.Images.SetKeyName(4, "");
            this.imageList1.Images.SetKeyName(5, "");
            this.imageList1.Images.SetKeyName(6, "");
            this.imageList1.Images.SetKeyName(7, "");
            // 
            // splitter4
            // 
            this.splitter4.Dock = System.Windows.Forms.DockStyle.Top;
            this.splitter4.Location = new System.Drawing.Point(0, 104);
            this.splitter4.Name = "splitter4";
            this.splitter4.Size = new System.Drawing.Size(408, 4);
            this.splitter4.TabIndex = 1;
            this.splitter4.TabStop = false;
            // 
            // panel11
            // 
            this.panel11.Controls.Add(this.label7);
            this.panel11.Controls.Add(this.txtFinalSOSL);
            this.panel11.Dock = System.Windows.Forms.DockStyle.Top;
            this.panel11.Location = new System.Drawing.Point(0, 0);
            this.panel11.Name = "panel11";
            this.panel11.Padding = new System.Windows.Forms.Padding(0, 25, 0, 0);
            this.panel11.Size = new System.Drawing.Size(408, 104);
            this.panel11.TabIndex = 0;
            // 
            // label7
            // 
            this.label7.Location = new System.Drawing.Point(0, 8);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(192, 16);
            this.label7.TabIndex = 13;
            this.label7.Text = "Final SOSL Statement";
            // 
            // txtFinalSOSL
            // 
            this.txtFinalSOSL.BackColor = System.Drawing.Color.White;
            this.txtFinalSOSL.Dock = System.Windows.Forms.DockStyle.Fill;
            this.txtFinalSOSL.Location = new System.Drawing.Point(0, 25);
            this.txtFinalSOSL.Multiline = true;
            this.txtFinalSOSL.Name = "txtFinalSOSL";
            this.txtFinalSOSL.ReadOnly = true;
            this.txtFinalSOSL.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.txtFinalSOSL.Size = new System.Drawing.Size(408, 79);
            this.txtFinalSOSL.TabIndex = 9;
            // 
            // splitter5
            // 
            this.splitter5.Dock = System.Windows.Forms.DockStyle.Top;
            this.splitter5.Location = new System.Drawing.Point(0, 104);
            this.splitter5.Name = "splitter5";
            this.splitter5.Size = new System.Drawing.Size(408, 4);
            this.splitter5.TabIndex = 1;
            this.splitter5.TabStop = false;
            // 
            // panel12
            // 
            this.panel12.Controls.Add(this.label10);
            this.panel12.Controls.Add(this.txtReturnFields);
            this.panel12.Dock = System.Windows.Forms.DockStyle.Top;
            this.panel12.Location = new System.Drawing.Point(0, 0);
            this.panel12.Name = "panel12";
            this.panel12.Padding = new System.Windows.Forms.Padding(0, 25, 0, 0);
            this.panel12.Size = new System.Drawing.Size(408, 104);
            this.panel12.TabIndex = 0;
            // 
            // label10
            // 
            this.label10.Location = new System.Drawing.Point(0, 8);
            this.label10.Name = "label10";
            this.label10.Size = new System.Drawing.Size(112, 16);
            this.label10.TabIndex = 10;
            this.label10.Text = "Return Field Spec";
            // 
            // txtReturnFields
            // 
            this.txtReturnFields.Dock = System.Windows.Forms.DockStyle.Fill;
            this.txtReturnFields.Location = new System.Drawing.Point(0, 25);
            this.txtReturnFields.Multiline = true;
            this.txtReturnFields.Name = "txtReturnFields";
            this.txtReturnFields.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.txtReturnFields.Size = new System.Drawing.Size(408, 79);
            this.txtReturnFields.TabIndex = 12;
            this.txtReturnFields.TextChanged += new System.EventHandler(this.txtReturnFields_TextChanged);
            // 
            // label11
            // 
            this.label11.Location = new System.Drawing.Point(8, 19);
            this.label11.Name = "label11";
            this.label11.Size = new System.Drawing.Size(104, 16);
            this.label11.TabIndex = 21;
            this.label11.Text = "Enter Search Query";
            // 
            // btnSend
            // 
            this.btnSend.Location = new System.Drawing.Point(376, 16);
            this.btnSend.Name = "btnSend";
            this.btnSend.Size = new System.Drawing.Size(96, 24);
            this.btnSend.TabIndex = 23;
            this.btnSend.Text = "Send Request";
            this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(480, 16);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(184, 16);
            this.label1.TabIndex = 26;
            // 
            // tabEditor
            // 
            this.tabEditor.Controls.Add(this.frmEditList1);
            this.tabEditor.Location = new System.Drawing.Point(4, 22);
            this.tabEditor.Name = "tabEditor";
            this.tabEditor.Size = new System.Drawing.Size(680, 487);
            this.tabEditor.TabIndex = 3;
            this.tabEditor.Text = "Data Editor";
            // 
            // frmEditList1
            // 
            this.frmEditList1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.frmEditList1.Location = new System.Drawing.Point(0, 0);
            this.frmEditList1.Name = "frmEditList1";
            this.frmEditList1.Size = new System.Drawing.Size(680, 487);
            this.frmEditList1.TabIndex = 0;
            // 
            // tabDocuments
            // 
            this.tabDocuments.Controls.Add(this.documentControl1);
            this.tabDocuments.Location = new System.Drawing.Point(4, 22);
            this.tabDocuments.Name = "tabDocuments";
            this.tabDocuments.Size = new System.Drawing.Size(680, 487);
            this.tabDocuments.TabIndex = 4;
            this.tabDocuments.Text = "Documents";
            // 
            // documentControl1
            // 
            this.documentControl1.AuthenticationMode = DocumentControl.DocumentControl.AUTHENTICATION_MODE.SESSION_ID;
            this.documentControl1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.documentControl1.documentName = "";
            this.documentControl1.folderID = "";
            this.documentControl1.Location = new System.Drawing.Point(0, 0);
            this.documentControl1.Mode = DocumentControl.DocumentControl.SHOW_MODE.OPEN_MODE;
            this.documentControl1.Name = "documentControl1";
            this.documentControl1.Padding = new System.Windows.Forms.Padding(5);
            this.documentControl1.ServerURL = null;
            this.documentControl1.SessionId = null;
            this.documentControl1.Size = new System.Drawing.Size(680, 487);
            this.documentControl1.TabIndex = 0;
            this.documentControl1.UserId = null;
            // 
            // contextMenu1
            // 
            this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuSControlEdit,
            this.menuSControlDelete,
            this.menuSControlPaste});
            // 
            // menuSControlEdit
            // 
            this.menuSControlEdit.Index = 0;
            this.menuSControlEdit.Text = "&Edit";
            // 
            // menuSControlDelete
            // 
            this.menuSControlDelete.Index = 1;
            this.menuSControlDelete.Text = "&Delete";
            // 
            // menuSControlPaste
            // 
            this.menuSControlPaste.Index = 2;
            this.menuSControlPaste.Text = "&Paste";
            // 
            // btnLogin
            // 
            this.btnLogin.Location = new System.Drawing.Point(8, 8);
            this.btnLogin.Name = "btnLogin";
            this.btnLogin.Size = new System.Drawing.Size(88, 24);
            this.btnLogin.TabIndex = 2;
            this.btnLogin.Text = "Login";
            this.btnLogin.Click += new System.EventHandler(this.btnLogin_Click);
            // 
            // statusBar1
            // 
            this.statusBar1.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.statusBar1.Location = new System.Drawing.Point(0, 553);
            this.statusBar1.Name = "statusBar1";
            this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
            this.sbpLoggedIn,
            this.sbpURL});
            this.statusBar1.ShowPanels = true;
            this.statusBar1.Size = new System.Drawing.Size(688, 24);
            this.statusBar1.TabIndex = 13;
            this.statusBar1.Text = "statusBar1";
            // 
            // sbpLoggedIn
            // 
            this.sbpLoggedIn.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Contents;
            this.sbpLoggedIn.Name = "sbpLoggedIn";
            this.sbpLoggedIn.Text = "Not Logged In";
            this.sbpLoggedIn.Width = 105;
            // 
            // sbpURL
            // 
            this.sbpURL.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
            this.sbpURL.Name = "sbpURL";
            this.sbpURL.Text = "URL:";
            this.sbpURL.Width = 566;
            // 
            // toolTip1
            // 
            this.toolTip1.AutoPopDelay = 5000;
            this.toolTip1.InitialDelay = 750;
            this.toolTip1.ReshowDelay = 750;
            // 
            // frmMain
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(688, 577);
            this.Controls.Add(this.tabControl1);
            this.Controls.Add(this.statusBar1);
            this.Controls.Add(this.btnLogin);
            this.Menu = this.mainMenu1;
            this.MinimumSize = new System.Drawing.Size(632, 584);
            this.Name = "frmMain";
            this.Padding = new System.Windows.Forms.Padding(0, 37, 0, 0);
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "sforce Explorer";
            this.panel1.ResumeLayout(false);
            this.panel3.ResumeLayout(false);
            this.panel2.ResumeLayout(false);
            this.panel5.ResumeLayout(false);
            this.panel5.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
            this.panel4.ResumeLayout(false);
            this.panel4.PerformLayout();
            this.tabControl1.ResumeLayout(false);
            this.tabSOQL.ResumeLayout(false);
            this.tabSOSL.ResumeLayout(false);
            this.tabSOSL.PerformLayout();
            this.panel6.ResumeLayout(false);
            this.panel7.ResumeLayout(false);
            this.panel8.ResumeLayout(false);
            this.panel9.ResumeLayout(false);
            this.panel10.ResumeLayout(false);
            this.panel11.ResumeLayout(false);
            this.panel11.PerformLayout();
            this.panel12.ResumeLayout(false);
            this.panel12.PerformLayout();
            this.tabEditor.ResumeLayout(false);
            this.tabDocuments.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.sbpLoggedIn)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.sbpURL)).EndInit();
            this.ResumeLayout(false);

		}
		#endregion

		[STAThread]
		static void Main() 
		{
			Application.Run(new frmMain());
		}

		public void AddNodeChild(TreeNode parent, TreeNode child) 
		{
			parent.Nodes.Add(child);
		}
		
		/// <summary>
		/// This is the function that sets up the form
		/// </summary>
		private void ActivateForm() 
		{
			//enable all our menus
			this.mnuSamplesShowUserInfo.Enabled = true;
			this.btnRunQuery.Enabled = true;
			this.mnuSamplesAddAccount.Enabled = true;
			this.mnuSamplesResetPassword.Enabled = true;
			this.mnuSamplesSetNewPassword.Enabled = true;
			this.mnuSamplesGetTimeStamp.Enabled = true;

			this.txtSOQL.Enabled = true;

			//Create our listener and wire up it's events
			//Listener = new LogTraceListener();
			//Listener.RequestMade+=new RequestEvent(Listener_RequestMade);
			//Listener.ResponseMade+=new ResponseEvent(Listener_ResponseMade);
			//System.Diagnostics.Trace.Listeners.Add(Listener);
			
			if (GlobalProperties.useProxy || GlobalProperties.noHTTPS) 
			{
				//The last time the user ran this he was using a proxy
				Utility.binding.Url = Utility.binding.Url.Replace("https:", "http:");
			} 
			else 
			{ 
				//since no proxy is being used, ensure that we are secure
				Utility.binding.Url = Utility.binding.Url.Replace("http:", "https:");
			}
			
			//Store the user preferences in the registry
			GlobalProperties.SaveSettings();

			//verify that we have a valid session id, if so, replace all the tabs
			if (GlobalProperties.useSessionId) 
			{
				try 
				{
					GlobalProperties.CheckSessionId(GlobalProperties.appServerURL, GlobalProperties.sessionId);
					addTabs();
				} 
				catch (Exception ex) 
				{
					System.Windows.Forms.MessageBox.Show("There was an error using the entered session information: \n\n" + ex.Message, "Session Id Not Valid", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
					new properties().ShowDialog();
				}
			} 
			else if (Utility.loginResult != null) 
			{
				addTabs();
			}

			//Initialize the documentControl
			this.documentControl1.ServerURL = GlobalProperties.endPoint;
			this.documentControl1.SessionId = GlobalProperties.sessionId;
			this.documentControl1.UserId = GlobalProperties.UserId;
			this.documentControl1.LoadFolders("");

		}

		private void addTabs()
		{
			if (this.tabControl1.Controls.Count == 0) 
			{
				this.tabControl1.Controls.Add(this.tabSOQL);
				this.tabControl1.Controls.Add(this.tabSOSL);
				this.tabControl1.Controls.Add(this.tabEditor);
				this.tabControl1.Controls.Add(this.tabDocuments);
				this.frmEditList1.InitialzeControl();
			}
		}

		
		private ArrayList ParseFieldList(String soqlText) 
		{
			//remove the select statement
			String temp = soqlText.Substring(soqlText.ToLower().IndexOf(" ")).Trim();
			//remove the everything after the field list
			temp = temp.Substring(0, temp.ToLower().IndexOf(" from "));
			//remove all the spaces
			temp = temp.Replace(" ", "");
			//return the tokenized array
			String[] temparray = temp.Split(",".ToCharArray());
			ArrayList al = new ArrayList();
			for (int i=0;i<temparray.Length;i++)
			{
				al.Add(temparray[i]);
			}
			return al;
		}

		DataSet ds = null;

		private DataTable CreateDataTable(ArrayList fieldList, sforce.QueryResult qr) 
		{
			DataTable dt = new DataTable(qr.records[0].type);
			for (int i=0;i<fieldList.Count;i++) 
			{
				dt.Columns.Add(fieldList[i].ToString());
				dt.Columns[fieldList[i].ToString()].DataType = Utility.GetFieldType(qr.records[0].type, fieldList[i].ToString());
				dt.Columns[fieldList[i].ToString()].AllowDBNull = Utility.FieldIsNullable(dt.TableName, fieldList[i].ToString());
				sforce.fieldType ft = Utility.GetField(qr.records[0].type, fieldList[i].ToString()).type;
				if (ft == sforce.fieldType.reference || ft == sforce.fieldType.id)
					dt.Columns[fieldList[i].ToString()].ExtendedProperties.Add("referenceTo", true);
				else
					dt.Columns[fieldList[i].ToString()].ExtendedProperties.Add("referenceTo", false);
				if (Utility.IsSystemColumn(dt.Columns[fieldList[i].ToString()])) 
					dt.Columns[fieldList[i].ToString()].ExtendedProperties.Add("systemfield", true);
				else
					dt.Columns[fieldList[i].ToString()].ExtendedProperties.Add("systemfield", false);
			}

			ArrayList records = Utility.ConvertQueryResultsToHashtable(qr);
			this.progressBar1.Value = 0;
			this.progressBar1.Step = 1;
			this.progressBar1.Maximum = qr.records.Length;
			for (int i=0;i<records.Count;i++) 
			{
				DataRow dr = dt.NewRow();
				Hashtable ht = (Hashtable)records[i];
				for (int j=0;j<dt.Columns.Count;j++) 
					dr[dt.Columns[j].ColumnName] = ht[dt.Columns[j].ColumnName.ToLower()];
				dt.Rows.Add(dr);
				this.progressBar1.PerformStep();
			}
			return dt;
		}

		private void btnRunQuery_Click(object sender, System.EventArgs e)
		{
			Cursor = Cursors.WaitCursor;
			try 
			{
				this.btnMore.Visible = false;
				Utility.binding.QueryOptionsValue = new sforce.QueryOptions();
				Utility.binding.QueryOptionsValue.batchSize = Convert.ToInt16(Application.UserAppDataRegistry.GetValue("batchSize", "500"));
				Utility.binding.QueryOptionsValue.batchSizeSpecified = true;

				sforce.QueryResult qr = Utility.binding.query(this.txtSOQL.Text);
				if (qr.size > 0) 
				{
					if (qr.size > qr.records.Length) this.btnMore.Visible = true;
					DataTable dt = CreateDataTable(ParseFieldList(this.txtSOQL.Text), qr);
					ds = new DataSet();
					ds.Tables.Add(dt);
					dataGrid1.DataSource = ds;
					dataGrid1.DataMember = dt.TableName;
					Utility.CreateDataGridGridTableStyle(dataGrid1);
					lblSummary.Text = dt.Rows.Count.ToString() + " of " + qr.size.ToString();
					lastQuery = qr;
				}
				else 
				{
					this.dataGrid1.DataSource = null;
					System.Windows.Forms.MessageBox.Show("No records matched query.", "Patner Sample", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
				}
			} 
			catch (Exception ex) 
			{
				System.Windows.Forms.MessageBox.Show("Query failed: " + ex.Message, "Partner Sample", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
			}
			Cursor = Cursors.Default;
		}

		private void GetNextSet() 
		{
			System.Data.DataTable dt = ((System.Data.DataSet)this.dataGrid1.DataSource).Tables[this.dataGrid1.DataMember];
			sforce.QueryResult qr = Utility.binding.queryMore(lastQuery.queryLocator);
			this.progressBar1.Value = 0;
			this.progressBar1.Maximum = qr.records.Length;
			this.progressBar1.Step = 1;
			for (int i=0;i<qr.records.Length;i++) 
			{
				this.progressBar1.PerformStep();
				DataRow dr = dt.NewRow();
				sforce.sObject record = qr.records[i];
				if (dr.Table.Columns.Contains("ID") )
					dr["Id"] = record.Id;
				for (int j=0;j<record.Any.Length;j++) 
				{
					System.Xml.XmlElement xEl = (System.Xml.XmlElement)record.Any[j];
					dr[xEl.LocalName] = xEl.InnerText;
				}
				dt.Rows.Add(dr);
			}
			if (!qr.done)
			{
				lastQuery = qr;
			} 
			else 
			{
				this.btnMore.Visible = false;
				lastQuery = null;
			}
			this.progressBar1.Value = 0;
			this.lblSummary.Text = dt.Rows.Count.ToString() + " of " + qr.size.ToString();
		}

		private void apiBrowserControl1_FieldDoubleClicked(object sender, System.EventArgs e) 
		{
			System.Windows.Forms.TreeNode tn = (System.Windows.Forms.TreeNode) sender;
			String objectName = tn.Parent.Parent.Text;
		}

		private void apiBrowserControl1_SelectListChanged(object sender, sforceExplorer.ApiBrowserControl.SelectListChangedArguments[] e)
		{

			ApiBrowserControl.SelectListChangedArguments[] slca = e;
			if (slca == null) 
			{
				this.txtSOQL.Text = "";
			} 
			else 
			{
				string table = slca[0].TableName;
				string fields = "";
				for (int i=0;i<slca[0].SelectedFields.Length;i++) 
				{
					fields += slca[0].SelectedFields[i] + ", ";
				}
				fields = fields.Substring(0, fields.Length - 2);
				if (txtSOQL.Text.ToLower().IndexOf(" where") >= 0)
					this.txtSOQL.Text = "Select " + fields.Trim() + " from " + table.Trim() + " " + this.txtSOQL.Text.Substring(this.txtSOQL.Text.ToLower().IndexOf(" where"));
				else 
					this.txtSOQL.Text = "Select " + fields.Trim() + " from " + table.Trim();
			}
		}


		private void btnMore_Click(object sender, System.EventArgs e)
		{
			Cursor = Cursors.WaitCursor;
			GetNextSet();
			Cursor = Cursors.Default;
		}

		private void menuItem2_Click(object sender, System.EventArgs e)
		{
			properties f = new properties();
			f.ShowDialog(this);
		}

		private void frmMain_Closed(object sender, EventArgs e)
		{
			InternalProxy.Stop();
			this.internalProxy = null;
			GlobalProperties.SaveSettings();
		}

		private void menuItem3_Click(object sender, System.EventArgs e)
		{
			GlobalProperties.useCompression = !GlobalProperties.useCompression;
			menuItem3.Checked = GlobalProperties.useCompression;
			Utility.binding.AcceptZippedResponse = GlobalProperties.useCompression;
			Utility.binding.SendZippedRequest = GlobalProperties.useCompression;
		}

		private void menuItem5_Click(object sender, System.EventArgs e)
		{
			GlobalProperties.useProxy = !GlobalProperties.useProxy;
			menuItem5.Checked = GlobalProperties.useProxy;
			if (GlobalProperties.useProxy || GlobalProperties.noHTTPS) 
			{
				Utility.binding.Proxy = new System.Net.WebProxy(GlobalProperties.proxyServer, Convert.ToInt16(GlobalProperties.proxyPort));
				Utility.binding.Url = Utility.binding.Url.Replace("https:", "http:");
			} 
			else 
			{
				Utility.binding.Proxy = null;
				Utility.binding.Url = Utility.binding.Url.Replace("http:", "https:");
			}
			GlobalProperties.SaveSettings();
		}


		private string GetFieldValue(string fieldName, System.Xml.XmlElement[] any) 
		{
			for (int i=0;i<any.Length;i++) 
			{
				if (any[i].LocalName.ToLower().Equals(fieldName.ToLower()))
				{
					string val;
					val = any[i].InnerText;
					val = val.Replace(",", "");
					val = val.Replace("\n", "n");
					return val;
				}
			}
			return "";
		}
		private string GetDateTimeSerialized(System.DateTime dateValue) 
		{
			return System.Xml.XmlConvert.ToString(dateValue);
		}


		private void frmMain_Activated(object sender, EventArgs e)
		{
			this.btnLogin.Enabled = !GlobalProperties.useSessionId;
			if (GlobalProperties.proxyServer.Length > 0 && GlobalProperties.proxyPort.Length > 0) 
				this.menuItem5.Enabled = true;
			else
				this.menuItem5.Enabled = false;
			if (GlobalProperties.useSessionId)
			{
				Utility.binding.AcceptZippedResponse = false;
				Utility.binding.SendZippedRequest = false;
				Utility.binding.Url = GlobalProperties.appServerURL;
				Utility.binding.SessionHeaderValue = new sforce.SessionHeader();
				Utility.binding.SessionHeaderValue.sessionId = GlobalProperties.sessionId;
				try 
				{
					sforce.GetUserInfoResult uir = Utility.binding.getUserInfo();
					string fullName = uir.userFullName;
					sforce.QueryResult qr = Utility.binding.query("Select username from user where id = '" + uir.userId + "'");
					string username = qr.records[0].Any[0].InnerText;
					this.sbpLoggedIn.Text = "SID: " + uir.userFullName + " (" + username + ")";
					this.sbpURL.Text = Utility.binding.Url;
				} 
				catch (Exception ex) 
				{
					System.Diagnostics.Trace.WriteLine(ex.Message);
				}
				ActivateForm();
			}
		}

		private void btnLogin_Click(object sender, System.EventArgs e)
		{
			frmLogin loginForm = new frmLogin();
			if (loginForm.ShowDialog(this).Equals(System.Windows.Forms.DialogResult.OK) )
			{
				sforce.GetUserInfoResult uir = Utility.binding.getUserInfo();
				string fullName = uir.userFullName;
				sforce.QueryResult qr = Utility.binding.query("Select username from user where id = '" + uir.userId + "'");
				string username = qr.records[0].Any[0].InnerText;
				this.sbpLoggedIn.Text = "Login: " + uir.userFullName + " (" + username + ")";
				this.sbpURL.Text = Utility.binding.Url;
				ActivateForm();
				this.apiBrowserControl1.Describe(true);
				this.apiBrowserControl2.Describe(false);
			}
		}


		private void btnRefresh_Click(object sender, System.EventArgs e)
		{
			this.apiBrowserControl1.Describe(true);
			this.apiBrowserControl2.Describe(false);
		}

		private void mnuSamplesAddAccount_Click(object sender, System.EventArgs e)
		{
			try 
			{
				frmAddAccount frmAdd = new frmAddAccount();
				frmAdd.loadIndustryCombo();
				frmAdd.ShowDialog();
			} 
			catch (Exception ex) 
			{
				System.Windows.Forms.MessageBox.Show("Add account failed: " + ex.Message, "Partner Sample", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
			}
		}

		private void mnuSamplesResetPassword_Click(object sender, System.EventArgs e)
		{
			try 
			{
				frmResetPassword frmReset = new frmResetPassword("reset");
				frmReset.ShowDialog();
			}
			catch (Exception ex) 
			{
				System.Windows.Forms.MessageBox.Show("Reset password failed: " + ex.Message, "Partner Sample", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
			}
		}

		private void mnuSamplesSetNewPassword_Click(object sender, System.EventArgs e)
		{
			try 
			{
				frmResetPassword frmReset = new frmResetPassword("set");
				frmReset.ShowDialog();
			}
			catch (Exception ex) 
			{
				System.Windows.Forms.MessageBox.Show("Set password failed: " + ex.Message, "Partner Sample", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
			}
		}

		private void mnuSamplesGetTimeStamp_Click(object sender, System.EventArgs e)
		{
			try 
			{
				sforce.GetServerTimestampResult tr = Utility.binding.getServerTimestamp();
				utilityCallResults ucr = new utilityCallResults();
				ucr.AddListViewItem("Server Time", tr.timestamp.ToLongDateString() + " " + tr.timestamp.ToLongTimeString());
				ucr.ShowDialog();
			}
			catch (Exception ex) 
			{
				System.Windows.Forms.MessageBox.Show("Get timestamp failed: " + ex.Message, "Partner Sample", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
			}
		}

		private void mnuSamplesShowUserInfo_Click(object sender, System.EventArgs e)
		{
				try 
				{
					sforce.GetUserInfoResult getUserInfoResult = Utility.binding.getUserInfo();
					utilityCallResults ucr = new utilityCallResults();
					ucr.AddListViewItem("Org Name", getUserInfoResult.organizationName);

					ucr.AddListViewItem("Org. Id", getUserInfoResult.organizationId);
					ucr.AddListViewItem("Currency Symbol", getUserInfoResult.currencySymbol);
					ucr.AddListViewItem("Is multi-currency", getUserInfoResult.organizationMultiCurrency.ToString());
					ucr.AddListViewItem("Default currency code", getUserInfoResult.userDefaultCurrencyIsoCode);
					ucr.AddListViewItem("Email address", getUserInfoResult.userEmail);
					ucr.AddListViewItem("Full name", getUserInfoResult.userFullName);
					ucr.AddListViewItem("ID", getUserInfoResult.userId);
					ucr.AddListViewItem("Language", getUserInfoResult.userLanguage);
					ucr.AddListViewItem("Locale", getUserInfoResult.userLocale);
					ucr.AddListViewItem("Time zone", getUserInfoResult.userTimeZone);
					ucr.ShowDialog();
				} 
				catch (Exception ex) 
				{
					System.Windows.Forms.MessageBox.Show("Get user info failed: " + ex.Message, "Partner Sample", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
				}
		}

		private void tabControl1_TabIndexChanged(object sender, EventArgs e)
		{
			this.apiBrowserControl2.MultiObjectSelect = true;
		}

		private void BuildSOSL() 
		{
			string temp = "FIND {" + this.txtSearchQuery.Text + "} ";
			if (this.cboSearchGroup.Text != "Global") 
				temp += "IN " + cboSearchGroup.Text.ToUpper();
			if (this.txtReturnFields.Text.Trim().Length > 0)
				temp += " RETURNING " + this.txtReturnFields.Text.Trim();
			this.txtFinalSOSL.Text = temp;

		}

		private void txtSearchQuery_TextChanged(object sender, System.EventArgs e)
		{
			BuildSOSL();
		}

		private void cboSearchGroup_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			BuildSOSL();
		}

		private void txtReturnFields_TextChanged(object sender, System.EventArgs e)
		{
			BuildSOSL();
		}

		private TreeNode GetNode(string nodeText) 
		{
			TreeNode x = null;
			if (tvRes.Nodes == null) 
			{
				x = new TreeNode(nodeText);
				tvRes.Nodes.Add(x);
				return x;
			} 
			else 
			{
				foreach (TreeNode y in tvRes.Nodes) 
				{
					if (y.Text == nodeText) return y;
				}
				x = new TreeNode(nodeText);
				tvRes.Nodes.Add(x);
				return x;
			}
		}

		private int SetImage(string type) 
		{
			//the imagelist contains the following images
			/**account.gif
			 * campaign.gif
			 * case.gif
			 * contact.gif
			 * contract.gif
			 * document.gif
			 * lead.gif
			 * opportunity.gif
			 */
			int retValue;

			switch (type.ToLower()) 
			{
				case "account":
					retValue = 0;
					break;
				case "campaign":
					retValue = 1;
					break;
				case "case":
					retValue = 2;
					break;
				case "contact":
					retValue = 3;
					break;
				case "contract":
					retValue = 4;
					break;
				case "document":
					retValue = 5;
					break;
				case "lead":
					retValue = 6;
					break;
				case "opportunity":
					retValue = 7;
					break;
				default:
					retValue = 8;
					break;
			}
			return retValue;
		}

		private void btnSend_Click(object sender, System.EventArgs e)
		{
			tvRes.Nodes.Clear();
			try 
			{
				sforce.SearchResult sr = Utility.binding.search(this.txtFinalSOSL.Text);
				if (sr.searchRecords == null) 
				{
					System.Windows.Forms.MessageBox.Show("No results found, try again.", "Partner Sample - SOSL Tester", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
					this.lblSummary.Text = "";
					return;
				} 
				else 
				{
					this.lblSummary.Text = sr.searchRecords.Length.ToString() + " records found...";
					this.progressBar1.Step = 1;
					this.progressBar1.Value = 0;
					this.progressBar1.Maximum = sr.searchRecords.Length;
					sforce.SearchRecord[] recs = sr.searchRecords;
					for (int i=0;i<recs.Length;i++) 
					{
						this.progressBar1.PerformStep();
						sforce.SearchRecord rec = recs[i];
						TreeNode x = GetNode(rec.record.type);
						TreeNode n = new TreeNode("Item");
						n.ImageIndex = SetImage(rec.record.type);
						n.SelectedImageIndex = n.ImageIndex;
						x.ImageIndex = n.ImageIndex;
						x.SelectedImageIndex = n.ImageIndex;
						sforce.sObject sobj = rec.record;
						for (int j=0;j<sobj.Any.Length;j++) 
						{
							TreeNode c = new TreeNode(sobj.Any[j].LocalName + " - " + sobj.Any[j].InnerText);
							c.ImageIndex = n.ImageIndex;
							c.SelectedImageIndex = c.SelectedImageIndex;
							n.Nodes.Add(c);
						}
						x.Nodes.Add(n);				
					}
				}
			} 
			catch (Exception ex) 
			{
				System.Windows.Forms.MessageBox.Show("SOSL Test returned and error:\n" + ex.Message, "Partner Sample - SOSL Tester", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
			}
		}

		private void apiBrowserControl2_SelectListChanged(object sender, ApiBrowserControl.SelectListChangedArguments[] e)
		{
			if (apiBrowserControl2.CurrentObject.searchable) 
			{
				ApiBrowserControl.SelectListChangedArguments[] slca = e;
				if (slca == null) 
				{
					this.txtReturnFields.Text = "";
				} 
				else 
				{
					string fieldSpec = "";
					for (int i=0;i<slca.Length;i++) 
					{
						string table = slca[i].TableName;
						string fields = "";
						for (int j=0;j<slca[i].SelectedFields.Length;j++) 
						{
							fields += slca[i].SelectedFields[j] + ", ";
					
						}
						fields = fields.Substring(0, fields.Length - 2);
						fieldSpec += slca[i].TableName + "(" + fields + "), ";
					}
					fieldSpec = fieldSpec.Substring(0, fieldSpec.Length - 2);
					this.txtReturnFields.Text = fieldSpec;
				}
			} 
			else { if (apiBrowserControl2.CurrentRootNode.Checked) apiBrowserControl2.CurrentRootNode.Checked = false; }
		}

		private void btnRefreshSOSL_Click(object sender, System.EventArgs e)
		{
			this.apiBrowserControl2.Describe(true);
		}

		private void documentControl1_DocumentSelected(string DocumentPath, string DocumentId, string FolderId, string DocumentName)
		{
			System.Diagnostics.Process p = new System.Diagnostics.Process();
			p.StartInfo.FileName = DocumentPath;
			p.Start();
		}

		private InternalProxy internalProxy;


		private void binding_OnQueryComplete(TimeSpan elapsedTime)
		{
			this.lblElapsedTime.Text = "Elapsed Time: " + elapsedTime.Hours.ToString().PadLeft(2, "0".ToCharArray()[0]) + ":" + elapsedTime.Minutes.ToString().PadLeft(2, "0".ToCharArray()[0]) + ":" + elapsedTime.Seconds.ToString().PadLeft(2, "0".ToCharArray()[0]) + "." + (elapsedTime.Milliseconds * 10).ToString().PadLeft(4, "0".ToCharArray()[0]);
		}

		private void menuMessageLogCopy_Click(object sender, System.EventArgs e)
		{
			System.Windows.Forms.RichTextBox tb = (System.Windows.Forms.RichTextBox)this.ActiveControl;
			System.Windows.Forms.Clipboard.SetDataObject(tb.SelectedText);
		}


	}
}
